*--------------------------------------------------------------------------------------------------------------	*
* RESEARCHERS:		EARN												   										*
* PROGRAMMED BY:	Linea Hasager																				*
* DESCRIPTION:		Main and robustness tests: RD estimates including control variables, different polynomials,	* 
*					kernels and municipality fixed effects. 													*
* CREATED:			Oct. 17, 2019																		   		*
* LAST MODIFIED:	May 25, 2022														       					*
*--------------------------------------------------------------------------------------------------------------	*
	
	
clear 
set more off

// Directories
global in "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Input"
global output "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Output"
global temp "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Temp"


*--------------------------------------------------------------------------------------------------------	

********************************
* 1) LOAD DATA
********************************

use "${in}\Analysis_sample.dta", replace




*RENAME VARIABLES
rename convicted_y18 convicted_y_18_sum
rename complex_job_y18 complex_job_y_18_sum
rename non_complex_job_y18 n_complex_job_y_18_sum



*OUTCOME VARIABLES
global outcomes loenmv_13_y_18 employment_y_18 complex_job_y_18 n_complex_job_y_18 convicted_y_18

*CONTROL VARIABLES
global x_control age age_sq unmarried female kids02 kids018 iraq afghanistan arabic dari danishf3 danishf2 danishf1 quota familyref other_permit

*Generate dummy variables for first municipality of residence
tabulate first_municipality, generate(dfirst_municipality)
*Use Copenhagen municipality as the reference group
global municipalityFE dfirst_municipality2-dfirst_municipality97



*********************************
*	 2) ESTIMATIONS
*********************************


foreach var in $outcomes {
	
*MAIN SPECIFICATION
rdrobust `var'_sum day_nr,  kernel(tri) c(0) p(1)
eststo `var'_sum_e
estadd scalar alpha=e(beta_p_l)[1,1]
estadd scalar bw=e(h_l)

*MAIN SPECIFICATION WITH MUNICIPALITY FE
rdrobust `var'_sum day_nr,  kernel(tri) c(0) p(1) covs($municipalityFE)
eststo `var'_sum_em
estadd scalar alpha=e(beta_p_l)[1,1]
estadd scalar bw=e(h_l)


*SIMPLE DIFFERENCE BETWEEN TREATED AND CONTROL 
rdrobust `var'_sum day_nr,   kernel(uni) c(0) p(0) 
eststo `var'_sum_dim
estadd scalar alpha=e(beta_p_l)[1,1]
estadd scalar bw=e(h_l)

*SIMPLE DIFFERENCE BETWEEN TREATED AND CONTROL  WITH MUNICIPALITY FE
rdrobust `var'_sum day_nr,  kernel(uni) c(0) p(0) covs($municipalityFE)
eststo `var'_sum_dimm
estadd scalar alpha=e(beta_p_l)[1,1]
estadd scalar bw=e(h_l)




*2ND ORDER POLYNOMIALS
rdrobust `var'_sum day_nr,  kernel(tri) c(0) p(2) 
eststo `var'_sum_p2
estadd scalar alpha=e(beta_p_l)[1,1]
estadd scalar bw=e(h_l)

*2ND ORDER POLYNOMIALS WITH MUNICIPALITY FE
rdrobust `var'_sum day_nr,  kernel(tri) c(0) p(2) covs($municipalityFE)
eststo `var'_sum_p2m
estadd scalar alpha=e(beta_p_l)[1,1]
estadd scalar bw=e(h_l)



*CONDITIONAL ON CONTROLS
rdrobust `var'_sum day_nr,  kernel(tri) c(0) covs($x_control) p(1)
eststo `var'_sum_c
estadd scalar alpha=e(beta_p_l)[1,1]
estadd scalar bw=e(h_l)

*CONDITIONAL ON CONTROLS WITH MUNICIPALITY FE
rdrobust `var'_sum day_nr,  kernel(tri) c(0) covs($x_control $municipalityFE) p(1)
eststo `var'_sum_cm
estadd scalar alpha=e(beta_p_l)[1,1]
estadd scalar bw=e(h_l)

}



*******************************************************************
*	3) TABLE WITH ESTIMATES FOR MAIN RESULTS AND FUNCTIONAL FORM
*******************************************************************


esttab  loenmv_13_y_18_sum_e employment_y_18_sum_e complex_job_y_18_sum_e n_complex_job_y_18_sum_e using "${output}\table2.tex", replace se(%10.3fc) b(%10.3fc) nolabel nomtitle varlabel (RD_Estimate "RD Estimate") noobs nolines nonum nonotes nogaps postfoot("") prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{5}{ccccc}}\toprule & Earnings & Employment & Complex Job & Non-Complex Job \\  & (1) & (2) & (3) & (4) \\ \midrule") star(* 0.10 ** 0.05 *** 0.01)  posthead("& \multicolumn{4}{c}{\emph{Panel a. Main (Linear)}} \\")  stats(alpha bw, fmt(%5.3fc %10.0fc) label("Mean of Untreated at Cutoff" "Bandwidth")) 


esttab loenmv_13_y_18_sum_dim employment_y_18_sum_dim complex_job_y_18_sum_dim n_complex_job_y_18_sum_dim using "${output}\table2.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "Estimate") stats(alpha bw, fmt(%5.3fc %10.0fc) label("Mean of Untreated" "Bandwidth"))  nolines nonum nonotes nogaps posthead("& & & & \\ & \multicolumn{4}{c}{\emph{Panel b. Treatment-Control Difference in Means}} \\") prehead("") postfoot("") star(* 0.10 ** 0.05 *** 0.01)


esttab loenmv_13_y_18_sum_p2 employment_y_18_sum_p2 complex_job_y_18_sum_p2 n_complex_job_y_18_sum_p2 using "${output}\table2.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "RD Estimate") noobs nolines nonum nonotes nogaps  posthead("& & & & \\ & \multicolumn{4}{c}{\emph{Panel c. 2nd Order}} \\") prehead("") postfoot("") star(* 0.10 ** 0.05 *** 0.01)  stats(alpha bw, fmt(%5.3fc %10.0fc) label("Mean of Untreated at Cutoff" "Bandwidth")) 


esttab loenmv_13_y_18_sum_c employment_y_18_sum_c complex_job_y_18_sum_c n_complex_job_y_18_sum_c using "${output}\table2.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "RD Estimate") stats(alpha bw N, fmt(%5.3fc %5.0fc %5.0fc) label("Mean of Untreated at Cutoff" "Bandwidth" "N")) nolines nonum nonotes nogaps posthead("& & & & \\ & \multicolumn{4}{c}{\emph{Panel d. Controls}} \\") prehead("") postfoot("\bottomrule\end{tabular}}") star(* 0.10 ** 0.05 *** 0.01)






******************************************************************
* 4) TABLE WITH ESTIMATES FOR MAIN RESULTS, INCL. MUNICIPALITY FE
******************************************************************


esttab  loenmv_13_y_18_sum_em employment_y_18_sum_em complex_job_y_18_sum_em n_complex_job_y_18_sum_em using "${output}\oa_table4.tex", replace se(%10.3fc) b(%10.3fc) nolabel nomtitle varlabel (RD_Estimate "RD Estimate") noobs nolines nonum nonotes nogaps postfoot("") prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{7}{cc}}\toprule & Earnings & Employment & Complex Job & Non-Complex Job \\  & (1) & (2) & (3) & (4) \\ \midrule") star(* 0.10 ** 0.05 *** 0.01)  posthead("& \multicolumn{4}{c}{\emph{Panel a. Main (Linear)}} \\")  stats(alpha bw, fmt(%5.3fc %15.0fc) label("Mean of Untreated at Cutoff" "Bandwidth")) 


esttab loenmv_13_y_18_sum_dimm employment_y_18_sum_dimm complex_job_y_18_sum_dimm n_complex_job_y_18_sum_dimm using "${output}\oa_table4.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "Estimate") stats(alpha bw, fmt(%5.3fc %15.0fc) label("Mean of Untreated" "Bandwidth")) nolines nonum nonotes nogaps posthead("& & & & \\ & \multicolumn{4}{c}{\emph{Panel b. Treatment-Control Difference in Means}} \\") prehead("") postfoot("")  star(* 0.10 ** 0.05 *** 0.01)


esttab loenmv_13_y_18_sum_p2m employment_y_18_sum_p2m complex_job_y_18_sum_p2m n_complex_job_y_18_sum_p2m using "${output}\oa_table4.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "RD Estimate") noobs nolines nonum nonotes nogaps  posthead("& & & & \\ & \multicolumn{4}{c}{\emph{Panel c. 2nd Order}} \\") prehead("") postfoot("") star(* 0.10 ** 0.05 *** 0.01) stats(alpha bw, fmt(%5.3fc %15.0fc) label("Mean of Untreated at Cutoff" "Bandwidth")) 


esttab loenmv_13_y_18_sum_cm employment_y_18_sum_cm complex_job_y_18_sum_cm n_complex_job_y_18_sum_cm using "${output}\oa_table4.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "RD Estimate") stats(alpha bw N, fmt(%5.3fc %15.0fc %5.0fc) label("Mean of Untreated at Cutoff" "Bandwidth" "N")) nolines nonum nonotes nogaps posthead("& & & & \\ & \multicolumn{4}{c}{\emph{Panel d. Controls}} \\") prehead("") postfoot("\bottomrule\end{tabular}}") star(* 0.10 ** 0.05 *** 0.01)



************************************************************
* 5) TABLE WITH ESTIMATES FOR CRIME RESULTS
************************************************************


esttab  convicted_y_18_sum_e using "${output}\oa_table5.tex", replace se(%10.3fc) b(%10.3fc) nolabel nomtitle varlabel (RD_Estimate "RD Estimate") noobs nolines nonum nonotes nogaps postfoot("") prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{2}{cc}}\toprule & Criminal Convictions \\  & (1) \\ \midrule") star(* 0.10 ** 0.05 *** 0.01)  posthead("& \multicolumn{1}{c}{\emph{Panel a. Main (Linear)}} \\")  stats(alpha bw, fmt(%5.3fc %10.0fc) label("Mean of Untreated at Cutoff" "Bandwidth"))


esttab convicted_y_18_sum_dim using "${output}\oa_table5.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "Estimate") stats(alpha bw, fmt(%5.3fc %10.0fc) label("Mean of Untreated" "Bandwidth"))   nolines nonum nonotes nogaps posthead("&  \\ & \multicolumn{1}{c}{\emph{Panel b. Treatment-Control Difference in Means}} \\") prehead("") postfoot("") star(* 0.10 ** 0.05 *** 0.01)


esttab convicted_y_18_sum_p2 using "${output}\oa_table5.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "RD Estimate") noobs nolines nonum nonotes nogaps  posthead("&  \\ & \multicolumn{1}{c}{\emph{Panel c. 2nd Order}} \\") prehead("") postfoot("")star(* 0.10 ** 0.05 *** 0.01)  stats(alpha bw, fmt(%5.3fc %10.0fc) label("Mean of Untreated at Cutoff" "Bandwidth")) 


esttab convicted_y_18_sum_c using "${output}\oa_table5.tex", append se(%10.3fc) b(%10.3fc) label nomtitle varlabel (RD_Estimate "RD Estimate") stats(alpha bw N, fmt(%5.3fc %5.0fc %5.0fc) label("Mean of Untreated at Cutoff" "Bandwidth" "N")) nolines nonum nonotes nogaps posthead("& \\ & \multicolumn{1}{c}{\emph{Panel d. Controls}} \\") prehead("") postfoot("\bottomrule\end{tabular}}") star(* 0.10 ** 0.05 *** 0.01)

